package cn.slipi.admin.common.contants;

import com.google.common.base.Joiner;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


/**
 * ZkPathConstants
 */
public class ZkPathConstants {

    public static final String API_PARENT_PATH = "/gateway/api";

    public static final String GRANT_PARENT_PATH = "/gateway/grant";

    public static final String PLUGIN_PARENT_PATH = "/gateway/plugin";

    public static final String RULE_PARENT_PATH = "/gateway/rule";

    public static final String SIGN_PARENT_PATH = "/gateway/sign";

    public static String buildApiZkPath(String apiCode) {
        return Joiner.on("/").skipNulls().join(API_PARENT_PATH, apiCode);
    }

    public static String buildGrantZkPath(String apiCode) {
        return Joiner.on("/").skipNulls().join(GRANT_PARENT_PATH, apiCode);
    }

    public static String buildPluginZkPath(String pluginName) {
        return Joiner.on("/").skipNulls().join(PLUGIN_PARENT_PATH, pluginName);
    }

    public static String buildRuleZkPath(final String pluginName) {
        return Joiner.on("/").skipNulls().join(RULE_PARENT_PATH, pluginName);

    }

    public static String buildBottomGrantZkPathWithoutEncode(final String apiCode, final String appKey) {
        return Joiner.on("/").skipNulls().join(GRANT_PARENT_PATH, apiCode, appKey);
    }

    public static String buildBottomGrantZkPath(final String apiCode, final String appKey) throws UnsupportedEncodingException {
        return Joiner.on("/").skipNulls().join(GRANT_PARENT_PATH, apiCode, URLEncoder.encode(appKey, StandardCharsets.UTF_8.name()));
    }

    public static String buildBottomRuleZkPathWithoutEncode(final String pluginName, final String ruleName) {
        return Joiner.on("/").skipNulls().join(RULE_PARENT_PATH, pluginName, ruleName);
    }

    public static String buildBottomRuleZkPath(final String pluginName, final String ruleName) throws UnsupportedEncodingException {
        return Joiner.on("/").skipNulls().join(RULE_PARENT_PATH, pluginName, URLEncoder.encode(ruleName, StandardCharsets.UTF_8.name()));
    }


}
